<?php

class Counter extends CActiveRecord {
    /**
     * The followings are the available columns in table 'z_counter':
     * @var integer $id_view
     * @var string $ip
     * @var string $referer
     * @var string $page
     * @var string $c_date
     */

    /**
     * Returns the static model of the specified AR class.
     * @return CActiveRecord the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'z_counter';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'id_view' => 'id',
                'ip' => 'ip',
                'referer' => 'От куда пришел',
                'page' => 'Страница',
                'c_date' => 'Дата',
                'date'=>'date',
                'user_agent'=>'О пользователе',
                'user'=>'Пользователь',
        );
    }
    public function info($visible=true) {
        
        $errors = true;
        if(Yii::app()->controller->id == 'site' && Yii::app()->controller->action->id == 'error') $errors = false;
        
        $counter = new Counter;
        $counter->ip = $_SERVER['REMOTE_ADDR'];
        if(!$errors) $counter->error = 1;
        $counter->referer = $_SERVER['HTTP_REFERER'];
        $counter->user_agent = $_SERVER['HTTP_USER_AGENT'];
        $counter->page = $_SERVER['REQUEST_URI'];
        $counter->user = Yii::app()->user->name;
        $counter->forward_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];


        $criteria = new CDbCriteria;
        $criteria->group = 'ip';
        $criteria->distinct = True;
        $rows = count(Counter::model()->findAll($criteria));

        $counter->save();

        if(Yii::app()->params->counter['visible']&&$visible) {
            echo '<div id="counter" style="width:'.Yii::app()->params->counter['width'].';border: 1px solid '.Yii::app()->params->counter['border'].'; background: '.Yii::app()->params->counter['background'].'; color: '.Yii::app()->params->counter['color'].'; font-size: '.Yii::app()->params->counter['font-size'].'">
            <h6>ZCounter</strong></h6>
            <div><span>'.$rows.'</span></div>
            <div><span>'.$counter->id_view.'</span></div>
                </div>';
        }


    }
    public function adminView() {

    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;

        $criteria->compare('id_view',$this->id_view);

        $criteria->compare('ip',$this->ip,true);

        $criteria->compare('referer',$this->referer,true);

        $criteria->compare('page',$this->page,true);

        $criteria->compare('c_date',$this->c_date,true);

        return new CActiveDataProvider('Counter', array(
                        'criteria'=>$criteria,
        ));
    }
}